package cn.com.myspring.framework.beans.support;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import cn.com.myspring.framework.beans.config.MYBeanDefinition;

public class MYBeanDefinitionReader {

    private List<String> registyBeanClasses = new ArrayList<String>();

    private Properties config = new Properties();

    //固定配置文件中的key，相对于xml的规范
    private final String SCAN_PACKAGE = "scanPackage";

    public MYBeanDefinitionReader(String... locations){
        //通过URL定位找到其所对应的文件，然后转换为文件流
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(locations[0].replace("classpath:",""));
        try {
            config.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        doScanner(config.getProperty(SCAN_PACKAGE));
    }

    /**
     * @Description 根据配置文件,扫描包路径
     * @param scanPackage
     */
    private void doScanner(String scanPackage) {
        // 转换文件路径
        URL url = this.getClass().getClassLoader().getResource("./" + scanPackage.replaceAll("\\.","/"));
        File classPath = new File(url.getFile());
        for (File file : classPath.listFiles()) {
            if(file.isDirectory()){
                doScanner(scanPackage + "." + file.getName());
            }else{
                if(!file.getName().endsWith(".class")){ continue;}
                // 获取 全类名
                String className = (scanPackage + "." + file.getName().replace(".class",""));
                // 将 全类名存入集合中
                registyBeanClasses.add(className);
            }
        }
    }

    public Properties getConfig(){
        return this.config;
    }

    // 将扫描到的配置信息转换成 MYBeanDefinition对象,便于IOC操作
    public List<MYBeanDefinition> loadBeanDefinitions(){
        List<MYBeanDefinition> result = new ArrayList<MYBeanDefinition>();
        try {
            for (String className : registyBeanClasses) {
                // 根据全类名 获取 类对象
                Class<?> beanClass = Class.forName(className);

                // 过滤接口类
                if(beanClass.isInterface()) { continue; }

                result.add(doCreateBeanDefinition(toLowerFirstCase(beanClass.getSimpleName()),beanClass.getName()));

                // 将类实现的接口类,也创建BeanDefinition存入集合中
                Class<?> [] interfaces = beanClass.getInterfaces();
                for (Class<?> i : interfaces) {
                    result.add(doCreateBeanDefinition(i.getName(),beanClass.getName()));
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }


    /**
     * @Description 创建一个MYBeanDefinition
     * @param factoryBeanName - 首字母小写的简单类名
     * @param beanClassName - 全类名
     * @return
     */
    private MYBeanDefinition doCreateBeanDefinition(String factoryBeanName,String beanClassName){
        MYBeanDefinition beanDefinition = new MYBeanDefinition();
        beanDefinition.setBeanClassName(beanClassName);
        beanDefinition.setFactoryBeanName(factoryBeanName);
        return beanDefinition;
    }


    /**
     * @Description 将简单类名首字母转换为小写
     * @param simpleName
     * @return
     */
    private String toLowerFirstCase(String simpleName) {
        char [] chars = simpleName.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }
}
